package framework;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This class is a stage for displaying cells.
 * 
 */
public class DisplayUtilStage {
	private JPanel displayPane;
	private SelfDrawCellInterface[] cs;

	/**
	 * Constructor that initializes panel
	 * 
	 * @param JFrame main - the main frame where the display is placed on
	 * @param int xsize - the preferred width of the display
	 * @param int ysize - the preferred height of the display
	 * @param GridLayout layout - the preferred layout of the cells on the display
	 * @param SelfDrawCell[] cs - the selfDrawCells
	 */
	public DisplayUtilStage(JFrame main, int xsize, int ysize,
			GridLayout layout, SelfDrawCellInterface[] cs) {
		// Create new panel
		displayPane = new JPanel();
		this.cs = cs;
		// Customize panel
		displayPane.setPreferredSize(new Dimension(xsize, ysize));
		displayPane.setLayout(layout);
		// Remove components on panel
		displayPane.removeAll();
		// Draw each cell on panel
		for (SelfDrawCellInterface c : cs) {
			c.drawCell(displayPane);
		}
		displayPane.setBorder(null);
		redraw();
		// Add display panel to frame
		main.add(displayPane);
	}

	/**
	 * Redraw the display panel
	 */
	public void redraw() {
		// Remove components on panel
		displayPane.removeAll();
		// Draw each cell on panel
		for (SelfDrawCellInterface c : cs) {
			c.drawCell(displayPane);
		}
		displayPane.setVisible(true);
		displayPane.revalidate();
		displayPane.repaint();
	}

	/**
	 * Returns the display panel
	 * 
	 * @return JPanel displayPane - the panel on which display is shown
	 */
	public JPanel getPane() {
		return displayPane;
	}
}
